<?php
ini_set("session.auto_start", "true");

ob_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <?php
        include_once 'inc/global.php';

        $tid = isset($_GET['t']) ? $_GET['t'] : 1;
        $tablename = $tables[$tid]['t'];
        $tabletitle = $tables[$tid]['c'];

        $formname = $tables[$tid]['f'];
        $gridname = $tables[$tid]['g'];
        $_SESSION['tid'] = $tid;
        //echo "tid : " . $_SESSION['tid'];

        $sclause = '';
        if (isset($_GET['s']))
            $sclause = $_GET['s'];
        ?>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><?php echo $tabletitle; ?></title>

        <link rel="stylesheet" type="text/css" href="css/easyui.css" />
        <link rel="stylesheet" type="text/css" href="css/icon.css" />
        <link rel="stylesheet" type="text/css" href="css/easyuiforms.css" />
        <script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
        <script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="../js/tiny_mce/tiny_mce.js"></script>
        <script type="text/javascript">
            var url;
            function newData(tid){
                $('#dlg').dialog('open').dialog('setTitle','New Data');
                $('#fm').form('clear');
                setTinyMce();
                url = 'editor_save.php?t='+tid;
            }
            function editData(tid){
                var row = $('#dg').datagrid('getSelected');
                if (row){
                    $('#dlg').dialog('open').dialog('setTitle','Edit Data');
                    $('#fm').form('load',row);

                    setTinyMce();
                    //tinyMCE.execCommand("mceAddControl", true, "ov_title");

                    url = 'editor_update.php?id='+row.id+'&t='+tid;
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: 'Please select data to edit'
                    });
                }
            }
            function editPrices() {
                var row = $('#dg').datagrid('getSelected');
                if (row){
                    window.open('editor_prices.php?t=6&id='+row.id, 'Prices', 'width=700,height=600,scrollbars=1,location=0');
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: 'Please select data to edit'
                    });
                }
            }
            function editItinerary() {
                var row = $('#dg').datagrid('getSelected');
                if (row){
                    window.open('editor_itinerary.php?t=7&id='+row.id, 'Itinerary', 'width=850,height=600,scrollbars=1,location=0');
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: 'Please select data to edit'
                    });
                }
            }
            function packagePics() {
                var row = $('#dg').datagrid('getSelected');
                if (row){
                    window.open('editor_packagepics.php?t=7&id='+row.id, 'Itinerary', 'width=850,height=600,scrollbars=1,location=0');
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: 'Please select data to edit'
                    });
                }
            }
            function saveData(){
                
                $('#fm').form('submit',{
                    url: url,
                    onSubmit: function(){
                        return $(this).form('validate');
                    },
                    success: function(result){
                        //alert(result);
                            
                        var result = eval('('+result+')');
                        
                        if (result.success){
                            $('#dlg').dialog('close');
                            $('#dg').datagrid('reload');
                            
                            $.messager.show({
                                title: 'Info',
                                msg: 'Data saved'
                            });
                            location.reload();
                        } else {
                            $.messager.show({
                                title: 'Error',
                                msg: result.msg
                            });
                        }
                    }
                });
                $('#dg').datagrid('reload');
            }
            function removeData(tid){
                var row = $('#dg').datagrid('getSelected');
                if (row){
                    $.messager.confirm('Confirm','Are you sure you want to remove this data?',function(r){
                        if (r){
                            $.post('editor_remove.php?t='+tid,{id:row.id},function(result){
                                
                                if (result.success){
                                    $('#dg').datagrid('reload');
                                } else {
                                    $.messager.show({
                                        title: 'Error',
                                        msg: result.msg
                                    });
                                }
                            },'json');
                        }
                    });
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: 'Please select data to delete'
                    });
                }
            }
            
            function deleteSelected(tid) {
                var rows = $('#dg').datagrid('getSelections');
                if (rows.length > 0) {
                    $.messager.confirm('Confirm','Are you sure you want to remove selected data?',function(r) 
                    {
                        if (r) 
                        {
                            for(i=0; i<rows.length; i++) 
                            {
                                var delid = rows[i].id;
                                $.post('editor_remove.php?t='+tid,{id:delid},function(result){
                                    if (result.success){
                                        $('#dg').datagrid('reload');
                                    } else {
                                        $.messager.show({
                                            title: 'Error',
                                            msg: result.msg
                                        });
                                    }
                                },'json');
                            }
                        }
                    }); 
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: 'Please select data to delete'
                    });
                }
            }
            
            function replyMail(tid) {
                var row = $('#dg').datagrid('getSelected');
                if (row){
                    $('#dlg_reply').dialog('open').dialog('setTitle','Reply');
                    $('#fm_reply').form('load',row);
                    setTinyMce();
                    url = 'sendreply.php?id='+row.id+'&tid='+tid;
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: 'Please select data to reply'
                    });
                }
            }            
            function sendReply() {
                alert(url);
                $('#fm_reply').form('submit',{
                    url: url,
                    onSubmit: function(){
                        return $(this).form('validate');
                    },
                    success: function(result){
                        var result = eval('('+result+')');
                        $.messager.show({
                            title: 'Error',
                            msg: result
                        });
                        
                        if (result.success){
                            $('#dlg_reply').dialog('close');
                            $('#dg').datagrid('reload');
                                                    
                            $.messager.show({
                                title: 'Info',
                                msg: 'Reply sent'
                            });
                        } else {
                            $.messager.show({
                                title: 'Error',
                                msg: result.msg
                            });
                        }
                    }
                });
            }
            function disableTinyMce() {
                tinyMCE.get('tr_visaprices').remove();
                tinyMCE.get('tr_requirement').remove();
                tinyMCE.get('tr_notes').remove();
            }
            
            function setTinyMce() {
                tinyMCE.init({
                    // General options
                    height: "200",
                    mode : "textareas",
                    //mode: "none",
                    theme : "advanced",
                    plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave,visualblocks",
                    theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
                    theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,|,link,unlink,image,|,forecolor,backcolor,|,tablecontrols,|,hr,removeformat,|,sub,sup,|,charmap",
                    theme_advanced_toolbar_location : "top",
                    theme_advanced_toolbar_align : "left",
                    theme_advanced_statusbar_location : "bottom",
                    theme_advanced_resizing : false,

                    extended_valid_elements : "hr[class|width|size|noshade]",
                    file_browser_callback : "ajaxfilemanager",
                    paste_use_dialog : false,
                    theme_advanced_resize_horizontal : true,
                    apply_source_formatting : true,
                    force_br_newlines : true,
                    force_p_newlines : false,
                    relative_urls : false,
                    remove_script_host : false,
                    oninit: function(){
                        tinyMCE.execCommand('mceRemoveControl', false, 'msg');
                        disableTinyMce();
                    }
                });
            }
            
            function ajaxfilemanager(field_name, url, type, win) {
                var view = 'thumbnail';
                var ajaxfilemanagerurl = "../js/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php?view=" + view;

                //window.open(ajaxfilemanagerurl);
                switch (type) {
                    case "image":
                        break;
                    case "media":
                        break;
                    case "flash":
                        break;
                    case "file":
                        break;
                    default:
                        return false;
                }

                var fileBrowserWindow = new Array();
                fileBrowserWindow["file"] = ajaxfilemanagerurl;
                fileBrowserWindow["title"] = "Ajax File Manager";
                fileBrowserWindow["width"] = "782";
                fileBrowserWindow["height"] = "440";
                fileBrowserWindow["close_previous"] = "no";

                tinyMCE.activeEditor.windowManager.open({
                    url: ajaxfilemanagerurl,// "../js/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php?view=" + view,
                    width: 782,
                    height: 440,
                    inline : "yes",
                    close_previous : "no"
                },{
                    window : win,
                    input : field_name
                });
                return false;
            }
            
            function clearSearch() {
                $('#txtsearch').val('');
                document.forms['searchform'].submit();
            }
        </script>
    </head>
    <body>
        <form method="get" name="searchform">

            <?php include 'header.php'; ?>

            <h2><?php echo $tabletitle; ?></h2>

            <input type="hidden" name="t" value="<?php echo $tid; ?>" />
            <?php if (($tid == 3) || ($tid == 2)) { ?>
                <div>
                    Search
                    <input type="text" id="txtsearch" name="s" value="<?php echo $sclause; ?>" style="width: 400px;"/>
                    <input type="submit" value="Search" style="width: 100px;" />
                    <input type="button" value="Clear" style="width: 100px;" onclick="clearSearch();" />
                </div>
                <br />
            <?php } ?>
        </form>

        <table border="1" width="100%">
            <tr>
                <td width="100%">
                    <?php include $gridname; ?>
                </td>
            </tr>
        </table>

        <div id="toolbar">
            <?php
            if (($tid != 5) && ($tid != 9)) {
                if ($tid != 10) {
                    ?>
                    <a href="#"
                       class="easyui-linkbutton"
                       iconCls="icon-add"
                       plain="true"
                       onclick="newData('<?php echo $tid; ?>'); return false;">New
                    </a>
                <?php } ?>
                <a href="#"
                   class="easyui-linkbutton"
                   iconCls="icon-edit"
                   plain="true"
                   onclick="editData('<?php echo $tid; ?>'); return false;">Edit
                </a>
                <?php
                if ($tid != 10) {
                    ?>
                    <a href="#"
                       class="easyui-linkbutton"
                       iconCls="icon-remove"
                       plain="true"
                       onclick="removeData('<?php echo $tid; ?>'); return false;">Delete
                    </a>
                    <?php
                }
            }

            if ($tid == 5) {
                ?>
                <a href="#"
                   class="easyui-linkbutton"
                   iconCls="icon-remove"
                   plain="true"
                   onclick="removeData('<?php echo $tid; ?>'); return false;">Delete
                </a>
                <?php
            }

            if ($tid == 3) {
                ?>
                <!--                <a href="#"
                                   class="easyui-linkbutton"
                                   iconCls="icon-edit"
                                   plain="true"
                                   onclick="editPrices(); return false;">Prices
                                </a>-->
                <a href="#"
                   class="easyui-linkbutton"
                   iconCls="icon-edit"
                   plain="true"
                   onclick="editItinerary(); return false;">Itineraries
                </a>
                <a href="#"
                   class="easyui-linkbutton"
                   iconCls="icon-edit"
                   plain="true"
                   onclick="packagePics(); return false;">Pictures
                </a>

                <a href="#"
                   class="easyui-linkbutton"
                   iconCls="icon-remove"
                   plain="true"
                   onclick="deleteSelected('<?php echo $tid; ?>'); return false;">Delete Selected
                </a>
                <?php
            }

            if ($tid == 2) {
                ?>
                <a href="#"
                   class="easyui-linkbutton"
                   iconCls="icon-remove"
                   plain="true"
                   onclick="deleteSelected('<?php echo $tid; ?>'); return false;">Delete Selected
                </a>                    
                <?php
            }

            if (($tid == 5) || ($tid == 9)) {
                ?>
                <a href="#"
                   class="easyui-linkbutton"
                   iconCls="icon-reply"
                   plain="true"
                   onclick="replyMail('<?php echo $tid; ?>'); return false;">Reply
                </a>

            <?php } ?>
        </div>



        <?php
        $style = "width:800px;height:400px;padding:10px 20px";
        if ($tid == 3) {
            $style = "width:1200px; height:600px; padding:10px 20px";
        }
        ?>

        <div id="dlg"
             class="easyui-dialog"
             style="<?php echo $style; ?>"
             closed="true" buttons="#dlg-buttons"">

             <div class="ftitle"><?php echo $tabletitle; ?></div>
            <form id="fm" method="post" novalidate>
                <?php include $formname; ?>
            </form>
        </div>


        <div id="dlg-buttons">
            <a href="#"
               class="easyui-linkbutton"
               iconCls="icon-ok"
               onclick="saveData()">Save</a>

            <a href="#"
               class="easyui-linkbutton"
               iconCls="icon-cancel"
               onclick="javascript:$('#dlg').dialog('close')">Cancel</a>
        </div>




        <div id="dlg_reply"
             class="easyui-dialog"
             style="<?php echo $style; ?>"
             closed="true" buttons="#dlg-buttons">

            <div class="ftitle"><?php echo $tabletitle; ?></div>
            <form id="fm_reply" method="post" novalidate>
                <?php include $formname; ?>
            </form>
        </div>

        <div id="dlg-buttons">
            <a href="#"
               class="easyui-linkbutton"
               iconCls="icon-ok"
               onclick="sendReply()">Send</a>

            <a href="#"
               class="easyui-linkbutton"
               iconCls="icon-cancel"
               onclick="javascript:location.reload();">Cancel</a>
        </div>
        <?php ob_end_flush(); ?>


    </body>
</html>